/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.mdc.hearts.ejb.gameLogic;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;
import static it.mdc.hearts.ejb.gameLogic.GlobalConstants.*;

/**
 *
 * @author Administrator
 */
public final class HeartsModel implements Serializable{
    class PlayerDeck implements Serializable{
        ArrayList<Card> cardsDeck;

        public PlayerDeck() 
        {
            cardsDeck = new ArrayList<Card>();
        }
    }
    
    final Semaphore mutex;
    
    PlayerDeck[] playerDecks = new PlayerDeck[PLAYER_NUMBER];
    PlayerDeck[] playerCardsTaken = new PlayerDeck[PLAYER_NUMBER];
    Card[] playerCardPlayed = new Card[PLAYER_NUMBER];
    int[] playerPoint = new int[PLAYER_NUMBER];
    
    int nCurrentPlayer;
    int nGameTurnStep;
    int nGameTurnSeed;
    int nGameTurn;
    
    boolean bHeartBreak;
    
    HeartsModel() 
    {
        mutex = new Semaphore(1, true);
        
        for(int i = 0; i < PLAYER_NUMBER; i++)
        {
            playerPoint[i]=0;
        }
        
        initGame();
    }
    
    void initGame()
    {
        try {
            mutex.acquire();
            Deck deck = new Deck();
            nCurrentPlayer = -1;
            nGameTurnStep = -1;
            nGameTurn = -1;
            bHeartBreak = false;
            
            for(int i = 0; i < PLAYER_NUMBER; i++)
            {
                PlayerDeck tempDeck = new PlayerDeck();
                deck.getPlayerDeck(tempDeck.cardsDeck);
                playerCardPlayed[i] = null;
                
                int nSeed = tempDeck.cardsDeck.get(0).getSeed();
                int nValue = tempDeck.cardsDeck.get(0).getValue();
                
                if((nSeed == CLUBS) && (nValue == 0))
                {
                    //playerCardPlayed[i] = tempDeck.cardsDeck.remove(0);
                    nCurrentPlayer = i;
                    nGameTurn = 0;
                    nGameTurnStep = 0;
                    nGameTurnSeed = -1;
                }
                
                playerDecks[i] = tempDeck;
                
                tempDeck = new PlayerDeck();
                playerCardsTaken[i] = tempDeck;
            }
            mutex.release();
        } catch (InterruptedException ex) {
            Logger.getLogger(HeartsModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public ArrayList<Card> getPlayerDeckTaken(int playerIndex) {
        ArrayList<Card> cardReturn = null;
         try {
            mutex.acquire();
            cardReturn = playerCardsTaken[playerIndex].cardsDeck;
            mutex.release();
        } catch (InterruptedException ex) {
            Logger.getLogger(HeartsModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return cardReturn;
    }
   
    public int getPlayerPoint(int playerIndex) {
        int nPoint = 0;
        try {
            mutex.acquire();
            nPoint = playerPoint[playerIndex];
            mutex.release();
        } catch (InterruptedException ex) {
            Logger.getLogger(HeartsModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nPoint;
    }

    public int getCurrentPlayer() {
        int nPlayer = 0;
        try {
            mutex.acquire();
            nPlayer = nCurrentPlayer;
            mutex.release();
        } catch (InterruptedException ex) {
            Logger.getLogger(HeartsModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nPlayer;
    }
    
    public ArrayList<Card> getPlayerDeck(int nPlayerIndex) {
         ArrayList<Card> cardReturn = null;
         try {
            mutex.acquire();
            cardReturn = playerDecks[nPlayerIndex].cardsDeck;
            mutex.release();
        } catch (InterruptedException ex) {
            Logger.getLogger(HeartsModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return cardReturn;
    }
    
    public Card getPlayedCard(int nPlayerIndex) {
        Card cardReturn = null;
         try {
            mutex.acquire();
            cardReturn = playerCardPlayed[nPlayerIndex];
            mutex.release();
        } catch (InterruptedException ex) {
            Logger.getLogger(HeartsModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return cardReturn;
    }

    public int getPlayerCardNumber(int nPlayerIndex) {
        int nCardNumber = 0;
        try {
            mutex.acquire();
            nCardNumber = playerDecks[nPlayerIndex].cardsDeck.size();
            mutex.release();
        } catch (InterruptedException ex) {
            Logger.getLogger(HeartsModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nCardNumber;
    }
}
